package dreampack.dataQuery;

import java.util.*;
import java.sql.*;
import dreampack.*;
import dreampack.dataType.*;
import dreampack.tool.*;

public class MonStarQuery {
	public MonStarQuery() {	}
	
	public static Collection QueryMonStarsByDate(DreamDB dreamDb, java.util.Date date, boolean bSimple) throws Exception {
		/* if bSimple is true : 只查寻每种"明星"中 score 值最高的人员，
		   if bSimple is false : 查寻当月所有获得"明星"称号的人员
		*/
		if (null == date)
			return null;
		ArrayList monstars = new ArrayList();
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		String ymon = DateTool.getFormatDate("yyyy-MM", date);
		try {
			con = dreamDb.getConnection();
			String sql = "";
			if (bSimple) {
				sql = "select * from t_ht_monthstar m1 where m1.score = (select max(m2.score) from t_ht_monthstar m2 where m1.type_id=m2.type_id and m2.dur_time like \"" + ymon + "%\")";	//查寻每组中 score 值最高的行
			} else {
				sql = "select * from t_ht_monthstar where dur_time like \"" + ymon + "%\" order by type_id, score desc";
			}
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				MonStarType monthStar = new MonStarType();
				monthStar.init(dreamDb, rs);
				monstars.add(monthStar);
			}
		} catch (Exception e) {
			;
		} finally {
			dreamDb.closeResultSet(rs);
			dreamDb.closeStatement(stmt);
			dreamDb.closeConnection(con);
		}
		return monstars;
	}
}
